package simple;

/**
 * 给定一个字符串，验证它是否是回文串，只考虑字母和数字字符，可以忽略字母的大小写。
 *
 * 说明：本题中，我们将空字符串定义为有效的回文串。
 *
 * @author yuxuan.hu
 * */
public class ValidPalindrome {
    class Solution {
        public boolean isPalindrome(String s) {
            int tail = s.length() - 1;
            int start = 0;
            while(start < tail){
                char leftChar = s.charAt(start);
                char rightChar = s.charAt(tail);
                if(!isValid(leftChar) || !isValid(rightChar)){
                    if(!isValid(leftChar)){
                        start++;
                    }else{
                        tail--;
                    }
                    continue;
                }
                if(leftChar != rightChar && toUp(leftChar) != toUp(rightChar)) {
                    return false;
                }
                start++;
                tail--;

            }
            return true;
        }

        private boolean isLetter(char c){
            return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
        }

        private boolean isValid(char c){
            return isLetter(c) || (c >= '0' && c <= '9');
        }

        private char toUp(char c){
            final char START_LOWER_LETTER = 'a';
            if(isLetter(c) && c >= START_LOWER_LETTER){
                c -= 32;
            }
            return c;
        }
    }
}
